package com.yanq.liancar.activity;
/*
 *  @创建者     Administrator
 *  @创建时间   2018/10/10  16:30
 *  @描述       ${TODO}
 */


import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.FindCommentAdapter;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.FindCommentTwoBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WholeCommentActivity extends BaseToolbarActivity {
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.img_nonews)
    ImageView imgNonews;
    @BindView(R.id.et_pinglun)
    EditText etPinglun;
    @BindView(R.id.tv_send)
    TextView tvSend;
    @BindView(R.id.tv_ReplyToPeople)
    TextView tvReplyToPeople;
    @BindView(R.id.ll_pinglun)
    LinearLayout llPinglun;
    private int newsId;
    private List<FindCommentTwoBean.DataBean> mDataBeanList = new ArrayList<>();
    private FindCommentAdapter mFindCommentAdapter;
    private int CommentID,replytype,objectid;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wholecomment);
        ButterKnife.bind(this);
        Utils.setStatusBar(this, true, false);
        mTvTitle.setText("全部评论");

        newsId = getIntent().getIntExtra("newsid", 0);

        initRecyclerview();
        getComment(newsId);
    }

    private void initRecyclerview() {
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mFindCommentAdapter = new FindCommentAdapter(this, mDataBeanList);
        mRecyclerView.setAdapter(mFindCommentAdapter);
        mFindCommentAdapter.setOnClickListener(new FindCommentAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int postion) {
//                if (isSoftShowing()){
//                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
//                }

                etPinglun.setFocusable(true);
                etPinglun.setFocusableInTouchMode(true);
                tvReplyToPeople.setText(mDataBeanList.get(postion).getCommentatorname());
                CommentID=mDataBeanList.get(postion).getCommentsid();
                replytype=1;
                if (!isSoftShowing()) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        });

    }

    //获取评论
    private void getComment(int newsId) {
        OkGo.<String>post(Config.s + "llc/api/selectCommentsReplys")
                .tag(this)
                .params("newsid", newsId + "")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        FindCommentTwoBean bean = gson.fromJson(response.body(), FindCommentTwoBean.class);
                        if (bean.isIsSuccess()) {
                            if (bean.getCode() == 200) {
                                mDataBeanList.clear();
                                mDataBeanList.addAll(bean.getData());
                                mFindCommentAdapter.notifyDataSetChanged();

                                imgNonews.setVisibility(View.GONE);
                            } else {
                                imgNonews.setVisibility(View.VISIBLE);
                                // RxToast.normal(bean.getOutputMessage());
                            }
                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }
                });
    }


    //发送新闻评论
    private void setComment() {
        OkGo.<String>post(Config.s + "llc/api/comments_reply")
                .tag(this)
                .params("commentsid", 0)
                .params("newsid", newsId)
                .params("token", UserBean.getcToken(this))
                .params("commentcontent", etPinglun.getText().toString().trim())
                .params("replytype", 0)
                .params("objectid", 0)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        BaseBean baseBean = gson.fromJson(response.body(), BaseBean.class);
                        if (baseBean.isIsSuccess()) {
                            etPinglun.setText("");
                            RxToast.info(baseBean.getOutputMessage());
                        }
                    }
                });
    }

    //发表对人的评论
    private void setCommentPeople() {
        OkGo.<String>post(Config.s + "llc/api/comments_reply")
                .tag(this)
                .params("commentsid", CommentID + "")
                .params("newsid", newsId + "")
                .params("token", UserBean.getcToken(this))
                .params("commentcontent", etPinglun.getText().toString().trim())
                .params("replytype", replytype)
                .params("objectid", 0)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        BaseBean bean = gson.fromJson(response.body(), BaseBean.class);
                        if (bean.isIsSuccess()) {
                            RxToast.success(bean.getOutputMessage());
                            etPinglun.setText("");
                            tvReplyToPeople.setText("");
                            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }
                });
    }
    @OnClick({R.id.tv_send, R.id.ll_pinglun})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_send:
                if (!TextUtils.isEmpty(etPinglun.getText().toString().trim())) {
                    if (!TextUtils.isEmpty(tvReplyToPeople.getText().toString())){
                        setCommentPeople();
                    }else {
                        setComment();
                    }
                } else {
                    RxToast.error("请输入评论内容");
                }
                break;
            case R.id.ll_pinglun:
                break;
        }
    }

    //判断软键盘是否显示
    private boolean isSoftShowing() {
        //获取当前屏幕内容的高度
        int screenHeight = getWindow().getDecorView().getHeight();
        //获取View可见区域的bottom
        Rect rect = new Rect();
        //DecorView即为activity的顶级view
        getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        //考虑到虚拟导航栏的情况（虚拟导航栏情况下：screenHeight = rect.bottom + 虚拟导航栏高度）
        // 选取screenHeight*2/3进行判断
        return screenHeight*2/3 > rect.bottom;
    }

}
